package br.edu.ufcg.lsd.wbis.ui.client.ui.loggedin.admin.searchusers;

import java.util.List;

import br.edu.ufcg.lsd.wbis.ui.client.beans.UserInfo;
import br.edu.ufcg.lsd.wbis.ui.client.beans.UserMatcher;
import br.edu.ufcg.lsd.wbis.ui.client.services.UserService;
import br.edu.ufcg.lsd.wbis.ui.client.ui.loggedin.DefaultPortal;
import br.edu.ufcg.lsd.wbis.ui.client.ui.loggedin.admin.DataMovingToolBar;
import br.edu.ufcg.lsd.wbis.ui.client.ui.loggedin.admin.GenericTabPanel;
import br.edu.ufcg.lsd.wbis.ui.client.ui.loggedin.admin.InfoShower;
import br.edu.ufcg.lsd.wbis.ui.client.ui.loggedin.admin.UserInfoPortlet;
import br.edu.ufcg.lsd.wbis.ui.client.ui.loggedin.admin.DataMovingToolBar.NextDataListener;
import br.edu.ufcg.lsd.wbis.ui.client.ui.loggedin.admin.DataMovingToolBar.PreviousDataListener;
import br.edu.ufcg.lsd.wbis.ui.client.ui.management.InfoManager;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.widgets.TabPanel;
import com.gwtext.client.widgets.layout.RowLayout;
import com.gwtext.client.widgets.layout.RowLayoutData;

/**
 * @author Renato Miceli
 */
public class ShowSearchedUsersTab extends GenericTabPanel implements InfoShower {

	private static class SearchUsersCallback implements AsyncCallback<List<UserInfo>> {

		public static final Integer LIMIT_OF_PORTLETS = 9;

		private final DefaultPortal portal;

		private final TabPanel centerPanel;

		private final int section;

		private final ShowSearchedUsersTab tab;

		private final InfoManager<UserInfo> loggedManager;

		public SearchUsersCallback(int section, DefaultPortal portal, ShowSearchedUsersTab tab, TabPanel centerPanel, InfoManager<UserInfo> loggedManager) {
			this.section = section;
			this.portal = portal;
			this.tab = tab;
			this.centerPanel = centerPanel;
			this.loggedManager = loggedManager;
		}

		/**
		 * {@inheritDoc}
		 */
		public void onFailure(Throwable caught) {
			// Do nothing!
		}

		/**
		 * {@inheritDoc}
		 */
		public void onSuccess(List<UserInfo> result) {
			if (result == null) {
				return;
			}
			if (section <= 0 || (section - 1) * LIMIT_OF_PORTLETS >= result.size()) {
				return;
			}
			this.portal.clear();
			try {
				for (int i = (section - 1) * LIMIT_OF_PORTLETS; i < result.size() && i < section * LIMIT_OF_PORTLETS; i++) {
					this.portal.addPortlet(new UserInfoPortlet(result.get(i), centerPanel, loggedManager));
					this.portal.doLayout();
				}
			} catch (IndexOutOfBoundsException e) {
				return;
			}
			this.tab.setSection(section);
		}

	}

	public static final String ITEM_ID = "SEARCHTAB";

	private DefaultPortal portal;

	private int section;

	private final TabPanel centerPanel;

	private final UserMatcher matcher;

	private final InfoManager<UserInfo> infoManager;

	public ShowSearchedUsersTab(UserMatcher matcher, TabPanel centerPanel, InfoManager<UserInfo> infoManager) {

		this.setTitle("Searched users");
		this.setLayout(new RowLayout());

		this.portal = new DefaultPortal();
		this.add(this.portal, new RowLayoutData("94%"));

		this.add(new DataMovingToolBar(new PreviousDataListener(this), new NextDataListener(this)));

		this.section = 1;
		this.matcher = matcher;
		this.centerPanel = centerPanel;
		this.infoManager = infoManager;
		
		UserService.Util.getInstance().searchUsers(this.infoManager.getInfo(), this.matcher,
				new SearchUsersCallback(this.section, this.portal, this, this.centerPanel, this.infoManager));
	}

	public void showPreviousData() {
		UserService.Util.getInstance().searchUsers(this.infoManager.getInfo(), this.matcher,
				new SearchUsersCallback(this.section - 1, this.portal, this, this.centerPanel, this.infoManager));
	}

	public void showNextData() {
		UserService.Util.getInstance().searchUsers(this.infoManager.getInfo(), this.matcher,
				new SearchUsersCallback(this.section + 1, this.portal, this, this.centerPanel, this.infoManager));
	}

	public void setSection(int section) {
		this.section = section;

	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getTabId() {
		return ITEM_ID;
	}

}